﻿//*** Guidance generated code ***//
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;

using Survey.Common;

namespace Survey.Filters.Restrictions
{
    public class FilterRestrictionProvider : IFilterRestrictionProvider
    {
        IDictionary<Type, IFilterRestrictionHandler> _handlers;

        public void AdjustFilter(IFilter filter)
        {
            if (filter.IsNull())
            {
                return;
            }

            // is there handler for this type
            IFilterRestrictionHandler handler;
            var filterType = filter.GetType();

            if (!Handlers.TryGetValue(filterType, out handler))
            {
                return;
            }

            handler.Handle(filter);

        }
        public virtual void Add(Type type, IFilterRestrictionHandler handler)
        {
            if (Handlers.ContainsKey(type))
            {
                return;
            }
            Handlers[type] = handler;
        }
        protected virtual IDictionary<Type, IFilterRestrictionHandler> Handlers
        {
            get { return _handlers ?? (_handlers = new Dictionary<Type, IFilterRestrictionHandler>()); }
            set { _handlers = value; }
        }
    }
}